﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include <QtWidgets/QWidget>
#include "QtGnufToolbar.h"
#include "GnufPallet.h"

#define PROPERTY_PALLET_NUNM 0
#define EDIT_TYPE_NUM 1

class QLineEdit;
class QPushButton;
class QLabel;
class QHBoxLayout;
namespace gfam
{
    class QtGnufToolbar;
    class GnufPropertyPallet;
    class FamilyTypeIntegratedComponent;
    class FamilyTypeNameComponent;
    class FamilyNameValuePairsComponent;

    enum class ePreviewType
    {
        eNone = -1,
        eStandard,
        eLong,
        eWide
    };

    class QtGnufPropertyPallet final : public QWidget
    {
        Q_OBJECT
    public:
        explicit QtGnufPropertyPallet(GnufPropertyPallet*);
        ~QtGnufPropertyPallet();

        gnuf::FamilyTypes * familyTypes() const { return m_familyTypes.familyTypes(); }
        gnuf::NameValuePairs * nameValuePairs() const { return m_nameValuePairs.nameValuePairs(); }

        void destroy();
        void setEnabled(bool enable);
        // 判断鼠标是否离开属性面板区域
        bool isMouseLeavePropertyPallet() const;
        // 判断是否有属性修改发生
        bool isPropertyValueChanged() const;
        void onPropertyValueChanged();
        void onApplyStatus();
        void refreshAppearance();

    public slots:
        void onApply();

    protected:
        virtual void resizeEvent(QResizeEvent *event) override;
        virtual void leaveEvent(QEvent *event) override;
        virtual bool eventFilter(QObject *obj, QEvent *ev);

    private:
        //修改多条属性条目的值之后,鼠标移开属性面板区域,保证最后一条修改的Text类型的属性值更新
        bool takeLastChangedEffect();
        QString applyBtnStyleSheet();

    private:
        QPushButton * m_qtApply;
        QtGnufToolbar m_familyTypes;
        QtGnufToolbar m_nameValuePairs;
        GnufPropertyPallet * m_gnufPropertyPallet;
    };

    class QtNameFamilyType final : public QWidget, public gnuf::IPalletEventHandler
    {
    public:
        explicit QtNameFamilyType(FamilyTypeNameComponent*, const std::wstring & name, bool wantToRename);
        virtual ~QtNameFamilyType();

        int popup(const std::wstring &);
        gnuf::Pallet* pallet() { return m_pallet; }
        void setName(const std::wstring &);
        std::wstring name() const;

        void refresh();

        void setRenameEnabled(bool enable);
    protected:
        virtual bool onFooterButtonClicked(gnuf::Pallet *) override;
        virtual void onHeaderButtonClicked(gnuf::Pallet *) override;
        void setStyle();

    private:
        FamilyTypeNameComponent* m_familyTypeNameComponent;
        QLineEdit * m_qtName;
        QPushButton * m_qtRename;
        gnuf::Pallet* m_pallet;
    };

    class QtEditFamilyType final : public QWidget, public gnuf::IPalletEventHandler
    {
    public:
        explicit QtEditFamilyType(FamilyNameValuePairsComponent*, const std::wstring & name);
        virtual ~QtEditFamilyType();

        gnuf::NameValuePairs * nameValuePairs() const { return m_nameValuePairs.nameValuePairs(); }
        void setName(const std::wstring& name);
        int popup(const std::wstring &, QWidget* parent = nullptr, bool canPreview = false, ePreviewType eType = ePreviewType::eNone);
        QLabel * previewGeometry();
        void refresh();

    protected:
        virtual bool onFooterButtonClicked(gnuf::Pallet *) override;
        virtual void onHeaderButtonClicked(gnuf::Pallet *) override;
    private:
        FamilyNameValuePairsComponent* m_familyNameValuePairsBuilder;
        QtNameFamilyType * m_qtName;
        QtGnufToolbar m_nameValuePairs;
        QLabel * m_previewGeometry;
        QHBoxLayout * m_mainLayout;
        ePreviewType m_previewType;
        QSize m_szResized;
    };

}
